package com.wang.xh.consumer.controller;

import cn.hutool.core.util.ObjectUtil;
import com.wang.xh.consumer.service.MemberService;
import com.wang.xh.consumer.vo.user.MemberVO;
import com.wang.xh.consumer.vo.user.UserLoginRequestVO;
import com.wang.xh.consumer.vo.user.UserLoginVO;
import com.wang.xh.common.constants.Constants;
import com.wang.xh.common.util.R;
import com.wang.xh.common.util.UserThreadLocal;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

@Slf4j
@Tag(name = "用户管理")
@RestController
@RequestMapping("user")
public class MemberController {

    @Resource
    private MemberService memberService;

    /**
     * C端用户登录--微信登录
     *
     * @param userLoginRequestVO 用户登录信息
     * @return 登录结果
     */
    @PostMapping("/login/wxMin")
    @Operation(description = "登录")
    public R<UserLoginVO> login(@RequestBody UserLoginRequestVO userLoginRequestVO) throws IOException {
        UserLoginVO login = memberService.login(userLoginRequestVO);
        return R.ok(login);
    }

    /**
     * 刷新token，校验请求头中的长令牌，生成新的长短令牌
     *
     * @param refreshToken 原令牌
     * @return 登录结果
     */
    @PostMapping("/refresh")
    @Operation(description = "刷新token")
    public R<UserLoginVO> refresh(@RequestHeader(Constants.GATEWAY.REFRESH_TOKEN) String refreshToken) {
        UserLoginVO loginVO = memberService.refresh(refreshToken);
        if (ObjectUtil.isEmpty(loginVO)) {
            return R.error("刷新token失败，请重新登录.");
        }
        return R.ok(loginVO);
    }

    @Operation(description = "更新个人信息")
    @PutMapping("profile")
    public R<Void> update(@RequestBody MemberVO vo) {
        memberService.update(vo);
        return R.ok();
    }

    @Operation(description = "我的信息")
    @GetMapping("profile")
    public R<MemberVO> profile() {
        //获取userid
        Long userId = UserThreadLocal.getUserId();
        MemberVO member = memberService.detail(userId);
        return R.ok(member);
    }

    @Operation(description = "解除我的信息")
    @DeleteMapping("profile")
    public R<Void> del() {
        memberService.del();
        return R.ok();
    }

//    /**
//     * 实名认证
//     *
//     * @param verifyVo 姓名 & 身份证号
//     * @return 是否通过认证
//     */
//    @Operation(description = "实名认证")
//    @PostMapping("realNameVerify")
//    public R<RealNameVerifyVO> realNameVerify(@RequestBody RealNameVerifyVO verifyVo) {
//        RealNameVerifyVO realNameVerifyVo = memberService.realNameVerify(verifyVo);
//        if (realNameVerifyVo.getFlag()) {
//            return R.success(realNameVerifyVo);
//        }
//        return R.error(realNameVerifyVo, "身份校验未通过");
//    }

}